#!/usr/bin/perl -w
##################################################################################################
# Purpose: Verify BOVPN tunnel can be established and PCs can communicate with eachother with main mode
# TestLinkID: 83260
# Traffic: eg. send traffic from NET2PC1 to NET4PC1 in BOVPN tunnel
# Functional TestBed: bovpn-sigle-route-dropin
# Feature: bovpn
# Function: bovpn
##################################################################################################
# $ARGV[0] --- $dut1_external_ip
# $ARGV[1] --- $dut1_mgmt_ip
# $ARGV[2] --- $dut1_trusted_subnet
# $ARGV[3] --- $dut2_external_ip
# $ARGV[4] --- $pc1_eth0_ip
# $ARGV[5] --- $pc1_eth1_ip
# $ARGV[6] --- $pc2_eth0_ip


##################################################################################################
# Author             Action                                             Date
# Junkai             created initial tests                          11/28/2011
##################################################################################################

use strict;
use warnings;
use diagnostics;
use log_case;
use XTM;
#use LinuxPC;
#$SIG{TERM} = $SIG{INT}=\&safe_exit ;

#******** Variable for DUT1---XTM1050
my $dut1_external_ip    = $ARGV[0] ? $ARGV[0] : "1.1.1.1";
my $dut1_mgmt_ip        = $ARGV[1] ? $ARGV[1] : "192.168.10.251";
my $dut1_trusted_subnet = $ARGV[2] ? $ARGV[2] : "2.2.2.0/24";

my $dut2_external_ip    = $ARGV[3] ? $ARGV[3] : "10.0.1.1";
my $dut2_trusted_subnet = $ARGV[4] ? $ARGV[4] : "20.0.0.0/24";

my $dut1_external_if="External";

my $vif_count = 260;
my $route_per_vif = 1;


my $total_routes = $vif_count * $route_per_vif;


my $dut1 = new XTM(eth1=>$dut1_mgmt_ip);
my $log = new log_case();

my @ret;
my $mode = "Main";
my $pre_shared = "WGChengduOffice";
my $vif_name = "vif";
#my $tunnel_name = "BOVPN.Tunnel";

# construct remote gateway ip list
my @gateway_list = ();
my @tunnel_list = ();


if(254 ** 2 < $total_routes){
    print "tunnel is too many, not enough IP to use.";
    exit 0;
}


#$dut1->cli->clear_bovpn();
#exit 0;


$dut2_trusted_subnet =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\/\d{2}/;
my @tnl_ip_base = ($1,$2,$3,$4);
my @gw_list = split(/\./, $dut2_external_ip);
#$gw_list[$#tmp_list -1 - $i] = 0;
my $cnt = 0;
for(my $i = 0; $i < 255; $i ++){
    for (my $j = 1; $j < 255; $j ++){
        goto OUT if ($cnt >= $vif_count);
        my $new_ip = $gw_list[0].".".$gw_list[1].".".($gw_list[2]+$i)% 255 .".".$j;
        push(@gateway_list, $new_ip);
        print $new_ip."\n";
        my @tmp_list = ();
        for(my $k = 0; $k < $route_per_vif; $k++){
            my $route_item = $tnl_ip_base[0].".".($gw_list[2]+$i) % 255 .".".$k.".0/24";
            print "$route_item\n";
            push(@tmp_list, $route_item);
        }
        push(@tunnel_list, @tmp_list);
        $cnt++;
    }
}
OUT:

@ret = $dut1->cli->connect_cli;
exec_cmd("config");
exec_cmd("policy");
for(my $i = 0; $i <= $#gateway_list; $i++){
    print "\n\n\n-----------------------------\n";
    print "      VIF [$i].. \n";
    print "-----------------------------\n";
    exec_cmd("bovpn-vif $vif_name.$i");
    exec_cmd("endpoint $gateway_list[$i] $gateway_list[$i] $dut1_external_ip $dut1_external_if pre-shared $pre_shared");
    exec_cmd("apply");
    #exec_cmd("phase1 mode Main");
    #exec_cmd("phase1 nat-traversal enable");
    #exec_cmd("phase1 transform MD5 AES 8 encryp-key-length 16 unit hour Diffie-Hellman-Group5");
    #exec_cmd("exit");
    
    for(my $j = $i*$route_per_vif; $j < $i*$route_per_vif + $route_per_vif; $j++){
        #print "tunnel local $j $tunnel_list[$j]\n";
        exec_cmd("vpn-route $tunnel_list[$j]");
        #exec_cmd("phase2 pfs enable dh-group5");
        #exec_cmd("phase2 proposals ESP-AES-SHA1 ");
        exec_cmd("apply");
        exec_cmd("exit");
    }
}

$dut1->cli->disconnect;
exit 0;

sub exec_cmd{
    my $cmd = shift;
    my @ret = ();
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $mon++;
    #printf("\n[+] %04d-%02d-%02d_%02d:%02d:%02d\n",$year+1900, $mon, $mday, $hour, $min, $sec);
    @ret = $dut1->cli->cmd("$cmd");
    #printf("\n[-] %04d-%02d-%02d_%02d:%02d:%02d\n",$year+1900, $mon, $mday, $hour, $min, $sec);
    if($ret[0] eq 'nok'){
        exit 0;
    }
}

sub safe_exit{
    print "disconnect and exit 99\n";
    $dut1->cli->disconnect;
    exit 99;
}
